package run.halo.app.core.extension.content;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
import java.math.BigDecimal;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import run.halo.app.extension.AbstractExtension;
import run.halo.app.extension.GVK;
import run.halo.app.extension.GroupVersionKind;

/**
 * <p>Product extension.</p>
 * <p>产品扩展，用于管理产品信息</p>
 *
 * @author halo-product-management
 * @since 2.0.0
 */
@Data
@ToString(callSuper = true)
@GVK(group = Constant.GROUP, version = Constant.VERSION, kind = Product.KIND,
    plural = "products", singular = "product")
@EqualsAndHashCode(callSuper = true)
public class Product extends AbstractExtension {

    public static final String KIND = "Product";

    public static final GroupVersionKind GVK = GroupVersionKind.fromExtension(Product.class);

    public static final String PRODUCT_TYPE_REF_LABEL = "content.halo.run/product-type-ref";

    @Schema(requiredMode = RequiredMode.REQUIRED)
    private ProductSpec spec;

    @Schema
    private ProductStatus status;

    @JsonIgnore
    public ProductStatus getStatusOrDefault() {
        if (this.status == null) {
            this.status = new ProductStatus();
        }
        return status;
    }

    @JsonIgnore
    public boolean isDeleted() {
        return getMetadata().getDeletionTimestamp() != null;
    }

    @Data
    public static class ProductSpec {
        
        @Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
        private String displayName;
        
        @Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
        private String productTypeRef;
        
        @Schema(requiredMode = RequiredMode.NOT_REQUIRED)
        private String description;
        
        @Schema(requiredMode = RequiredMode.NOT_REQUIRED)
        private List<String> images;
        
        @Schema(requiredMode = RequiredMode.NOT_REQUIRED)
        private BigDecimal price;
        
        @Schema(requiredMode = RequiredMode.NOT_REQUIRED)
        private BigDecimal samplePrice;
        
        @Schema(requiredMode = RequiredMode.NOT_REQUIRED)
        private Integer minOrderQuantity;
        
        @Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "0")
        private Integer priority;
        
        @Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "true")
        private Boolean published;
    }

    @Data
    public static class ProductStatus {
        
        /**
         * 产品的永久链接
         */
        private String permalink;
        
        /**
         * 观察到的版本号
         */
        private Long observedVersion;
        
        /**
         * 产品类型显示名称（冗余字段，用于快速显示）
         */
        private String productTypeName;
    }
}